<!--
    限时购
 * @Date: 2020-03-16 10:52:48
 * @LastEditors: 黄栋
 * @LastEditTime: 2020-04-22 09:59:47
 * @FilePath: /cloudAdmin/src/views/market/promotion/feature/feature.vue
 --> 
 
<template>
  <!-- 产品列表 -->
  <div class="sec1">
    <el-card>
      <el-form :model="form" :inline="true" size="small" label-width="80px">
        <el-form-item label="商品名称">
          <el-input v-model="form.keywords" style="width:120px" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="活动时间">
          <el-date-picker
            placeholder="请选择"
            style="width:200px"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
            class="dateBox"
            v-model="form.start_time"
          ></el-date-picker>
          <span style="margin:0 5px">-</span>
          <el-date-picker
            placeholder="请选择"
            style="width:200px"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
            class="dateBox"
            v-model="form.end_time"
          ></el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="商品名称">
            <el-select>
                <el-option value="1">进行中</el-option>
                <el-option value="1">已结束</el-option>
            </el-select>
        </el-form-item>-->
        <el-form-item>
          <el-button @click="getDataList" type="primary">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="addOrUpdateHandle('')" type="primary">添加产品</el-button>
        </el-form-item>
      </el-form>
      <div style="margin-top:10px"></div>
      <el-table
        ref="multipleTable"
        :data="tableData"
        size="mini"
        :header-cell-style="{background:'#F7F7F7',color:'#1E1E1E'}"
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
      >
        <el-table-column label="商品" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-image
              style="width: 30px; height: 30px"
              :src="scope.row.thumb"
              :preview-src-list="[scope.row.thumb]"
            ></el-image>
            <span style="margin-left: 10px">{{ scope.row.title }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="sell" width="100" sortable label="实际销量" show-overflow-tooltip></el-table-column>
        <el-table-column prop="stock" sortable label="总库存" show-overflow-tooltip></el-table-column>
        <el-table-column prop="price" label="一口价" show-overflow-tooltip></el-table-column>
        <el-table-column label="活动时间" width="280px" show-overflow-tooltip>
          <template slot-scope="{row}">
            <div>
              {{row.start_time}}
              <span>-</span>
              {{row.end_time}}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="use_num" label="上架" show-overflow-tooltip>
          <template slot-scope="{row}">
            <el-switch :value="row.status==1" @change="changeStatus(row)" />
            <span class="shopStatus" style="margin-left:10px">{{row.status=='1'?'已上架':'未上架'}}</span>
          </template>
        </el-table-column>
        <el-table-column align="ceneter" label="操作">
          <template slot-scope="{row}">
            <el-button type="text" size="mini" @click="addOrUpdateHandle(row.id)">编辑</el-button>
            <el-button type="text" size="mini" @click="deleteHandle(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-top:10px"></div>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="form.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="form.page_num"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import {
  get_ac_goods_list,
  edit_goods_status,
  del_goods_info
} from "@/utils/api/market";
export default {
  props: {},
  data() {
    return {
      showTop: false,
      tableData: [],

      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      form: {
        page: 1,
        start_time: "",
        page_num: 10,
        end_time: "",
        keywords: ""
      }
    };
  },
  activated() {
    this.getDataList();
  },
  methods: {
    /**获取列表数据 */
    getDataList() {
      this.dataListLoading = true;
      get_ac_goods_list(this.form)
        .then(data => {
          this.dataListLoading = false;
          if (data.code == 200) {
            this.tableData = data.data.list;
            this.totalPage = data.data.total;
          } else {
            this.tableData = [];
            this.totalPage = 0;
          }
        })
        .catch(() => {
          this.dataListLoading = false;
          this.$message.error("服务器错误，请联系管理员");
        });
    },
    /**新增 / 修改 */
    addOrUpdateHandle(id) {
      //   this.$emit("editShop", id);
      this.$router.push({ path: "/featurePublish", query: { id: id } });
    },
    /**删除 */
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.id;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(() => {
          del_goods_info({ id: ids }).then(data => {
            if (data && data.code == 200) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                }
              });
            } else {
              this.$message.error(data.message);
            }
          });
        })
        .catch(() => {});
    },
    /**商品上下架 */
    changeStatus(data) {
      edit_goods_status({
        id: data.id,
        status: data.status == "1" ? "2" : "1"
      }).then(res => {
        if (res.code == 200) {
          this.getDataList();
        } else {
          this.$message.error(res.message);
        }
      });
    },
    // 每页数
    handleSizeChange(val) {
      this.form.page_num = val;
      this.form.page = 1;
      this.getDataList();
    },
    // 当前页
    handleCurrentChange(val) {
      this.form.page = val;
      this.getDataList();
    },
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less" scoped>
</style>